package objbo.xmlparser.model.modifier;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.tree.DefaultAttribute;

import objbo.xmlparser.model.bean.ElementBean;
import objbo.xmlparser.model.modeler.ElementModeler;
import objbo.xmlparser.model.modeler.XMLFileModeler;

public class ElementModiFier
{
	XMLFileModeler fileModeler = null;

	public ElementModiFier(XMLFileModeler fileModeler)
	{
		this.fileModeler = fileModeler;
	}

	public boolean elementAdd(ElementBean parentBean, String name)
	{


		boolean isDone = false;
		String path = null;
		int index = 0;
		Document document = null;
		Element element = null;

		try
		{
			path = parentBean.getPath();
			index = parentBean.getIndex();

			document = this.fileModeler.getFileBean().getDocument();
			element = (Element) document.selectNodes(path).get(index);

			element.addElement(name);

			isDone = true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}


		return isDone;
	}


	public boolean elementRemove(ElementBean parentBean, int subIndex)
	{
		Document document = null;
		Element element = null;
		Element subElement = null;


		String path = null;
		int index = 0;


		try
		{
			path = parentBean.getPath();
			index = parentBean.getIndex();

			document = this.fileModeler.getFileBean().getDocument();
			element = (Element) document.selectNodes(path).get(index);
			subElement = (Element) element.elements().get(subIndex);
		}

		catch (Exception e)
		{
			e.printStackTrace();
		}




		return element.remove(subElement);
	}



	public boolean elementRemove(ElementBean bean)
	{


		boolean isDone = false;
		String path = null;
		int index = 0;
		Document document = null;
		Element element = null;
		Element parentElement = null;

		try
		{
			path = bean.getPath();
			index = bean.getIndex();

			document = this.fileModeler.getFileBean().getDocument();
			element = (Element) document.selectNodes(path).get(index);
			parentElement = element.getParent();
			parentElement.remove(element);


			isDone = true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}


		return isDone;
	}


	public boolean attributeAdd(ElementBean bean, String name, String data)
	{
		boolean isDone = false;

		ElementModeler elementModeler = null;
		Attribute attribute = null;

		try
		{
			elementModeler = new ElementModeler();
			//attribute = new DefaultAttribute(name, data);
			bean.setAttrebute(name,data);
			//bean.getAttrebutes().add((Node)attribute);
			
			//System.out.println(bean.getAttrebutes().size());

			isDone = elementModeler.setElementData(fileModeler.getFileBean().getDocument(), bean);


		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



		return isDone;
	}
	public boolean attributeModify(ElementBean bean, String name, String data, int index)
	{
		boolean isDone = false;

		ElementModeler elementModeler = null;
		Attribute attribute = null;

		
		try
		{
			elementModeler = new ElementModeler();
			
			attribute = new DefaultAttribute(name, data);

			bean.getAttrebutes().set(index, attribute);
			
//			attribute = bean.getAttrebutes().get(index);
//			if(attribute.getName() != name)
//				{
//					attribute.setName(name);
//				}
//			attribute.setData(data);

			isDone = elementModeler.setElementData(fileModeler.getFileBean().getDocument(), bean);


		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



		return isDone;
	}
	public boolean attributeRemove(ElementBean bean, int index)
	{
		boolean isDone = false;

		ElementModeler elementModeler = null;
		Attribute attribute = null;

		try
		{
			elementModeler = new ElementModeler();
			
			System.out.println(bean.getAttrebutes().size());
			
			bean.getAttrebutes().remove(index);
			
			System.out.println(bean.getAttrebutes().size());
						
			isDone = elementModeler.setElementData(fileModeler.getFileBean().getDocument(), bean);


		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



		return isDone;
	}
	public boolean nameModify(ElementBean bean, String name)
	{
		boolean isDone = false;

		ElementModeler elementModeler = null;

		try
		{
			elementModeler = new ElementModeler();

			bean.setName(name);

			isDone = elementModeler.setElementData(fileModeler.getFileBean().getDocument(), bean);


		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



		return isDone;
	}
	public boolean contentModify(ElementBean bean, String content)
	{
		boolean isDone = false;

		ElementModeler elementModeler = null;

		try
		{
			elementModeler = new ElementModeler();

			bean.setContent(content);

			isDone = elementModeler.setElementData(fileModeler.getFileBean().getDocument(), bean);


		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



		return isDone;
	}

}
